//import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import './pages/indexpage.dart';
import './router/Router.dart';
import 'splash.dart';
import 'package:provider/provider.dart';
import 'provied/tit.dart';
import 'provied/cartmodel.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:shared_preferences/shared_preferences.dart';
//import 'package:amap_location_fluttify/amap_location_fluttify.dart';
//import 'package:amap_core_fluttify/amap_core_fluttify.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:fluwx/fluwx.dart';
import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import './model/cartmodellist.dart';
import './config/api.dart';
import 'dart:convert';
import './provied/cartmodel.dart';
Future<void> main() async {
//   Provider(
//   create: (_) => CartProvide(),
//   child: Text('你好')
// );
//   runApp(MyApp());

  runApp(
    MultiProvider(
      providers: [
         ChangeNotifierProvider(create: (_) => CartProvide()),
        // ChangeNotifierProvider<CartProvide>.value(
        //   value: CartProvide(),
        // )
        //  ChangeNotifierProvider<CartProvide>(create: (_) => CartProvide()),
      ],
      child: MyApp(),
    ),
    //MyApp()
  );
  await enableFluttifyLog(false);
  await AmapService.instance.init(
    iosKey: '7a04506d15fdb7585707f7091d715ef4',
    androidKey: '41ca227720f6ae92b4f48ad7d7bb5f2f',
  );
}

class MyApp extends StatelessWidget {
  // const MyApp({Key key}) : super(key: key);
  @override
  void initState() async{
    getlist();
    add(1);
     this.checkPersmission();
    //_yantoken();
    Addressl().getAddress();
    
  }
  //登录验证token
  void _yantoken()async{
    gettoken().then((value) async => {
    //  print(value.data['message']['username']),
      if(value.data['err']==200){
       addtoken(value.data['message']['username'])
      }
    });
  }
  void checkPersmission() async {
    // 申请权限
    Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.location]);
    // 申请结果
    PermissionStatus permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.location);
    if (permission == PermissionStatus.granted) {
      //_getLocation();
    } else {
      bool isOpened = await PermissionHandler().openAppSettings();//打开应用设置
    }
  }

  @override
  Widget build(BuildContext context) {
   // _yantoken();
    // ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
    //ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
    return Container(
      child: MaterialApp(
        title: 'woshi',
        debugShowCheckedModeBanner: false,
        initialRoute: '/',
        theme: ThemeData(primaryColor: Colors.pink[300],platform: TargetPlatform.iOS,),
        //   home: IndexPage(),
        home: SplashScreen(),
        onGenerateRoute: onGenerateRoute,
      ),
    );
  }

  addtoken(v) async{
    print('我是token${v}');
    var c=v;
    SharedPreferences prefs=await SharedPreferences.getInstance();
    prefs.setString('username', c);
   // print('我是token登录来的${json.decode(prefs.getString('username'))}');
  }
}
